package algorithm.leetcode.simple;

/*
 给定一个非空数组，返回此数组中第三大的数。如果不存在，则返回数组中最大的数。要求算法时间复杂度必须是O(n)。
 */

/**
 * @author jack.wu
 * @version 1.0
 * @date 2020/10/27
 */
public class Question414 {

    public static void main(String[] args) {
        int[] nums = {3, 2, 1};
        Question414 question = new Question414();
        int thirdMax = question.thirdMax(nums);
        System.out.println(thirdMax);
        System.out.println(Integer.MIN_VALUE);
    }

    public int thirdMax(int[] nums) {
        long one = Long.MIN_VALUE;
        long two = Long.MIN_VALUE;
        long three = Long.MIN_VALUE;

        for (int num : nums) {
            if (num == one || num == two || num == three) {
                continue;
            }
            if (num > one) {
                three = two;
                two = one;
                one = num;
            } else if (num > two && num < one) {
                three = two;
                two = num;
            } else if (num > three && num < two) {
                three = num;
            }
        }
        if (three == Long.MIN_VALUE) {
            return new Long(one).intValue();
        }
        return new Long(three).intValue();
    }
}
